package com.zut.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/*
*
* 自定义元数据对象处理器
*
* */

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpSession session;


    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充insert。。。");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long empId = (Long) session.getAttribute("employee");

        metaObject.setValue("createUser", empId);
        metaObject.setValue("updateUser", empId);

        Long userId = (Long) session.getAttribute("user");
        metaObject.setValue("createUser", userId);
        metaObject.setValue("updateUser", userId);


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充update。。。");
        log.info(metaObject.toString());

        Long empId = (Long) session.getAttribute("employee");
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", empId);

        Long userId = (Long) session.getAttribute("user");
        metaObject.setValue("updateUser", userId);

    }
}
